function d() {
    var c = document.getElementsByClassName('file');
    var num = [];
    var bo = [];
    for (var i = 0; i < c.length; i++) {
        if (c[i].value == '') {
            $('.err').eq(i).css({display : 'inline-block'});
        }
        num.push(c[i].value);
    }

    var bor = document.getElementsByClassName('bor');
    for (var i = 0; i < bor.length; i++) {
        if (bor[i].innerHTML != '') {
            bo.push(bor[i].innerHTML);
        } else {
            bo.push(bor[i].value);
        }
    }

    console.log(num, bo);
    return num, bo;
}

var addPdf;
var addImage;
var closeLayer;
$('.btn1,.btn2,.btn3,.btn4').on('click', function(e) {
    var index;

    function addFileNode(e, $node) {
        $target = $(e.target);
        if ($target.hasClass('btn1')) {
        	$node.children("input[name='type']").val('1');
            $('.rtop').append($node);
        }
        else if ($target.hasClass('btn2')) {
        	$node.children("input[name='type']").val('2');
            $('.rcen').append($node);
        }
        else if ($target.hasClass('btn3')) {
        	$node.children("input[name='type']").val('3');
            $('.rbtm').append($node);
        }
        else if($target.hasClass('btn4')){
        	$node.children("input[name='type']").val('4');
            $('.rbinss').append($node);
        }
        tuneTableHeight();
    }

    addPdf = function() {
        var str = '<div class="up user-added">'
        	+ '<input type="text" value="2" name="type" class="hidden-field">'
        	+ '<input type="text" value="-1" name="fileId" class="hidden-field" />'
        	+ '<input type="text" value="0" name="required" class="hidden-field">'
        	+ ' <span class="required-flag" style="opacity: 0;">*</span>'
        	+ ' <span class="name">名称</span>'
        	+ ' <input type="text" class="bor" name="bor" value="">'
        	+ '<input type="text" value="1" name="isPDF" class="hidden-field">'
        	+ ' </input><span class="img">文件:</span>'
        	+ ' <span class="fi" style="width: 112px;background: url(' + g_rootPath + '/Stlye/img/PDF.png) no-repeat;background-size: 100% auto;"><input type="file" name="files" class="file" accept="application/pdf"/>'
        	+ ' </span><span class="err"></span></div>';
        addFileNode(e, $(str));
        closeLayer();
    };

    addImage = function() {
        var str = '<div class="up user-added">'
        	+ '<input type="text" value="2" name="type" class="hidden-field">'
        	+ '<input type="text" value="-1" name="fileId" class="hidden-field" />'
        	+ '<input type="text" value="0" name="required" class="hidden-field">'
        	+ ' <span class="required-flag" style="opacity: 0;">*</span>'
        	+ ' <span class="name">名称</span>'
            + ' <input type="text" class="bor" name="bor" value="">'
            + ' <input type="text" value="2" name="isPDF" class="hidden-field">'
            + ' </input><span class="img">图片:</span>'
            + ' <span class="fi"><input type="file" name="files" class="file" accept="image/*"/>'
            + ' </span><span class="err"></span></div>';
        addFileNode(e, $(str));
        closeLayer();
    };

    closeLayer = function() {
        layer.close(index);
    };

    var $content = $("<div class='select-menu'></div>");
    $content.append('<div class="select-menu-item" onclick="addPdf();">新增PDF文件<div>');
    $content.append('<div class="select-menu-item" onclick="addImage();">新增图片上传<div>');
    $content.append('<div class="select-menu-item" onclick="closeLayer();"">取消<div>');
    index = layer.open({
        type: 1,
          shade: 0.6,
          shadeClose: false,
          closeBtn: 0,
          title: ["选择新增类型", 'padding: 0;text-align: center;'], //不显示标题
          content: $content.html(), //捕获的元素
          cancel: function(index){
              layer.close(index);
          }
    });
});
function tuneTableHeight() {
    var paddings = 10;
    $("div.ltop").css("height",
            $("div.rtop").innerHeight() - paddings + "px");
    $("div.lcen").css("height",
            $("div.rcen").innerHeight() - paddings + "px");
    $("div.lbtm").css("height",
            $("div.rbtm").innerHeight() - paddings + "px");
    $("div.lbinss").css("height",
            $("div.rbinss").innerHeight() - paddings + "px");
}
$(window).ready(function() {
    tuneTableHeight();
});

//$("#save").click(function(e) {
//    var $allMaterials = $("#materials-form .right .up");
//    // Update the type of user added ones.
//    for (var $material in $allMaterials) {
//        if ($material.hasClass("user-added")) {
//            var $typeElem = $material.children("input[name='type']");
//            var type = parseInt($typeElem.val());
//            type += 10;
//            $typeElem.val(type);
//        }
//    }
//    $("#materials-form").submit();
//});

$(".rtop,.rcen,.rbtm,.rbinss").on(
        "change",
        "input[name='files']",
        function(e) {
            $target = $(e.target);
            var fileName = $target.val();
            var $parent = $target.parent().parent();
            $parent.children(".local-file-name").remove();
            if (fileName) {
                $parent.append($("<span class='local-file-name'>"
                        + fileName + "</span>"));
            }
        });
